<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class DateUtils {
	private $secondsInHour = 3600;
	public static $FRIENDLY_DATE_FORMAT = "D, M d, Y";
	public static function computeHours($date1, $date2) {
		$t1 = strtotime($date1);
		$t2 = strtotime($date2);
		if($t2 > $t1){
			$diff = $t2 - $t1;
			return $diff/$secondsInHour;
		} else {
			return false;
		}
		
	}
	
	public static function formatDate($date,$format="Y-m-d"){
		return date($format, strtotime($date));
	}
	
	public static function getNumberOfDays($date1, $date2){
		$t1 = strtotime($date1);
		$t2 = strtotime($date2);
		
		$days = floor(abs($t2 - $t1)/$secondsInHour * 24);
	}
	
	public static function decimal_to_time($decimal) {
	    $hours = floor((int)$decimal / 60);
	    $minutes = floor((int)$decimal % 60);
	    $seconds = $decimal - (int)$decimal; 
	    $seconds = round($seconds * 60); 
	 
	    //return str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" . str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":" . str_pad($seconds, 2, "0", STR_PAD_LEFT);
	    return str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":" . str_pad($seconds, 2, "0", STR_PAD_LEFT) .":00";
	}
	
	public static function dec_to_time($decimal) {
		$hours = floor((int)$decimal);
		$minutes = ($decimal - $hours) * 60;
		$seconds = ceil(($minutes - floor((int)$minutes)) * 60);
	    //$hours = floor((int)$decimal / 60);
	    //$minutes = floor((int)$decimal % 60);
	    //$seconds = $decimal - (int)$decimal; 
	    //$seconds = round($seconds * 60); 
	 
	    return str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" . str_pad(ceil((int)$minutes), 2, "0", STR_PAD_LEFT) . ":" . str_pad($seconds, 2, "0", STR_PAD_LEFT);
	}
	
	
	
	/**
	 * Convert time into decimal time.
	 *
	 * @param string $time The time to convert
	 *
	 * @return integer The time as a decimal value.
	 */
	public static function time_to_decimal($time) {
		if($time != '') {
			$timeArr = explode(':', $time);
			$decTime = ($timeArr[0]*60) + ($timeArr[1]) + ($timeArr[2]/60);
		} else {
			$decTime = 0;
		}

	    return $decTime;
	}
	public static function time_to_his($time){
		return date('H:i:s',$time);
	}
	
	public static function timeDifference($date1, $date2){
		$time1 = strtotime($date1);
		$time2 = strtotime($date2);
		if($time2 > $time1){
			return $time2 - $time1;
		}else 
			return false;
	}
	
	public static function theSameDay($date, $day){
		$time1 = strtotime($date);
		$d = strtolower(date('l',$time1));
		return (trim(strtolower($day)) == $d);
	}
	
}